/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Collir.java
 *
 * Created on 15/03/2011, 12:06:31
 */
package vistas;

import Renderers.LabelRenderer;
import comu.Utilitats;
import controlador.AbonatControlador;
import controlador.AbonoControlador;
import controlador.ModeAplicacioControlador;
import controlador.ProduccioControlador;
import controlador.UnitatsMesuraControlador;
import controlador.autogenerats.exceptions.IllegalOrphanException;
import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.Abonat;
import entity.Abono;
import entity.ModeAplicacio;
import entity.Produccio;
import entity.Producte;
import entity.UnitatsMesura;
import java.io.IOException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import vistas.dialogs.DialogSituarParcela;
import vistas.textImg.Textos;

/**
 *
 * @author David
 */
public final class Abonar extends javax.swing.JFrame implements Observer {

    JFrame menu;
    Abonat abonat;
    Produccio produccio;
    
    AbonoControlador abonos;
    AbonatControlador abonats;
    ProduccioControlador produccions;
    ModeAplicacioControlador modesAplicacio;
    UnitatsMesuraControlador unitatsMesura;
    
    DialogSituarParcela situarParcela;

    /** Creates new form Collir */
    Abonar(JFrame menu, Abonat abonat, Produccio produccio) {
        this.menu = menu;
        this.abonat = abonat;
        this.produccio = produccio;

        unitatsMesura = new UnitatsMesuraControlador();
        modesAplicacio = new ModeAplicacioControlador();
        produccions = new ProduccioControlador();
        abonos = new AbonoControlador();
        abonats = new AbonatControlador();

        initComponents();
        llistaAbonos.setCellRenderer(new LabelRenderer());
        Utilitats.centrarVentana(this);
        try {
            if (this.abonat != null) {
                update(null, this.abonat);
            } else if (this.produccio != null) {
                update(null, this.produccio);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        llistaAbonos = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        btAvui = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        cbUnitat = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        btCancelar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        ftfTotal = new javax.swing.JFormattedTextField();
        ftfPerCada = new javax.swing.JFormattedTextField();
        lblBultos = new javax.swing.JLabel();
        ftfBultos = new javax.swing.JFormattedTextField();
        btAceptar = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        btMenu = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        lblMapaCamp = new javax.swing.JLabel();
        dcDataAbonat = new com.toedter.calendar.JDateChooser();
        jLabel10 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        lblComposicioAbono = new javax.swing.JLabel();
        lblQuantitatAbono = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        lblNomAbono = new javax.swing.JLabel();
        cbModeAplicacio = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        llistaAbonos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        llistaAbonos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                llistaAbonosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(llistaAbonos);

        jLabel3.setText(Textos.ABONAR_LABEL_DATA);

        btAvui.setText(Textos.PLANTAR_BOTO_AVUI);
        btAvui.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAvuiActionPerformed(evt);
            }
        });

        jLabel2.setText(Textos.ABONAR_LABEL_UNITATMESURA);

        cbUnitat.setModel(Utilitats.getComboBoxModel(unitatsMesura.findUnitatsMesuraEntitiesForPlantar()));
        cbUnitat.setAutoscrolls(true);
        cbUnitat.setEnabled(false);
        cbUnitat.setRenderer(new LabelRenderer());
        cbUnitat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbUnitatActionPerformed(evt);
            }
        });

        jLabel1.setText(Textos.ABONAR_LABEL_QUANTITAT);

        btCancelar.setText(Textos.COMU_BOTO_CANCELAR);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        jLabel5.setText(Textos.ABONAR_LABEL_PER_CADA);

        ftfTotal.setText("0");
        ftfTotal.setFont(Textos.FONT_GRAN);

        ftfPerCada.setEditable(false);
        ftfPerCada.setText("0");
        ftfPerCada.setEnabled(false);
        ftfPerCada.setFont(Textos.FONT_GRAN);
        ftfPerCada.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ftfBultosKeyTyped(evt);
            }
        });

        lblBultos.setText(Textos.ABONAR_LABEL_BULTOS);

        ftfBultos.setText("0");
        ftfBultos.setFont(Textos.FONT_GRAN);
        ftfBultos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ftfBultosKeyTyped(evt);
            }
        });

        btAceptar.setText(Textos.COMU_BOTO_ACCEPTAR);
        btAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAceptarActionPerformed(evt);
            }
        });

        jLabel7.setText(Textos.ABONAR_LABEL_MODE_APLICACIO);

        btMenu.setText(Textos.COMU_BOTO_MENU);
        btMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMenuActionPerformed(evt);
            }
        });

        jLabel8.setText(vistas.textImg.Textos.PLANTAR_LABEL_PRODUCTES);

        lblMapaCamp.setText(" ");
        lblMapaCamp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblMapaCampMouseClicked(evt);
            }
        });

        dcDataAbonat.setDate(GregorianCalendar.getInstance().getTime());

        jLabel10.setText(Textos.ADMIN_ABONO_NOM_ABONO);

        jLabel15.setText(Textos.ADMIN_ABONO_COMPOSICIO);

        jLabel16.setText(Textos.ADMIN_ABONO_QUANTITAT);

        lblComposicioAbono.setText("   ");

        lblQuantitatAbono.setText("   ");

        jLabel17.setText(Textos.ADMIN_LABEL_DESCRIPCIO);

        lblNomAbono.setText("   ");

        cbModeAplicacio.setModel(Utilitats.getComboBoxModel(modesAplicacio.findModeAplicacioEntities()));
        cbModeAplicacio.setAutoscrolls(true);
        cbModeAplicacio.setRenderer(new LabelRenderer());

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel17)
                    .add(jLabel16)
                    .add(layout.createSequentialGroup()
                        .add(jLabel15)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(lblComposicioAbono))
                    .add(layout.createSequentialGroup()
                        .add(jLabel10)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(lblNomAbono))
                    .add(layout.createSequentialGroup()
                        .add(24, 24, 24)
                        .add(lblQuantitatAbono))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 276, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(dcDataAbonat, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 162, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btAvui, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 97, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel5)
                            .add(jLabel1)
                            .add(lblBultos)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(cbUnitat, 0, 262, Short.MAX_VALUE)
                            .add(ftfBultos, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, ftfTotal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                            .add(ftfPerCada, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .add(jLabel7)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(cbModeAplicacio, 0, 265, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(lblMapaCamp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 222, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(btMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 129, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, btCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 129, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, btAceptar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(jLabel8)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(layout.createSequentialGroup()
                                .add(10, 10, 10)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jLabel3)
                                    .add(btAvui, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)))
                            .add(dcDataAbonat, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(cbUnitat, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblBultos)
                            .add(ftfBultos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel5)
                            .add(ftfPerCada, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(ftfTotal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel7)
                            .add(cbModeAplicacio, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(btAceptar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(lblMapaCamp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 182, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(55, 55, 55))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 363, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel17)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel10)
                            .add(lblNomAbono))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel15)
                            .add(lblComposicioAbono))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jLabel16)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblQuantitatAbono)
                            .add(btMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        if (produccio != null) {
            produccio = null;
            btMenuActionPerformed(evt);
        } else {
            produccio = null;
            update_Abonos();
        }
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAceptarActionPerformed

        Abono abono = (Abono) llistaAbonos.getSelectedValue();
        if (abono != null) {
            if (produccio != null) {
                try {
                    Date data = dcDataAbonat.getDate();
                    Integer quantitat = Integer.valueOf(ftfTotal.getText());
                    if (quantitat != 0) {
                        ModeAplicacio modeAplicio = (ModeAplicacio) cbModeAplicacio.getSelectedItem();
                        if(modeAplicio!= null){
                            if (abonat == null) {
                                abonat = new Abonat(null);
                                abonat.setDataAbonat(data);
                                abonat.setQuantitat(quantitat);
                                abonat.setAbonoId(abono);
                                abonat.setModeAplicacioId(modeAplicio);
                                abonat.setProduccioId(this.produccio);
                                abonats.create(abonat);
                                btCancelarActionPerformed(evt);
                                update(null, null);
                            } else {
                                abonat.setDataAbonat(data);
                                abonat.setQuantitat(quantitat);
                                abonat.setAbonoId(abono);
                                abonat.setModeAplicacioId(modeAplicio);
                                abonats.edit(abonat);
                                btCancelarActionPerformed(evt);
                                Thread.sleep(200);
                                btMenuActionPerformed(evt);
                            }
                        }else{
                            JOptionPane.showMessageDialog(this, Textos.ABONAR_INFO_ABONAT_MODE_APLICACIO, "", JOptionPane.WARNING_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, Textos.ABONAR_INFO_ABONAT_QUANTITAT, "", JOptionPane.WARNING_MESSAGE);
                    }
                } catch (IllegalOrphanException ex) {
                    Logger.getLogger(Abonar.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(Abonar.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(Abonar.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                JOptionPane.showMessageDialog(this, Textos.ABONAR_INFO_ABONAT_PRODUCCIO, "", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, Textos.ABONAR_INFO_ABONAT_ABONO, "", JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_btAceptarActionPerformed

    private void cbUnitatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbUnitatActionPerformed
        UnitatsMesura unitatMesura = (UnitatsMesura) cbUnitat.getSelectedItem();
        if (unitatMesura != null) {
            lblBultos.setText(unitatMesura.getNom());
        }
    }//GEN-LAST:event_cbUnitatActionPerformed

    private void btMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMenuActionPerformed
        if (menu != null) {
            if (General.class.isInstance(menu)) {
                ((General) menu).update(null, null);
            }
            menu.setVisible(true);
            this.setVisible(false);
        }
    }//GEN-LAST:event_btMenuActionPerformed

    private void ftfBultosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ftfBultosKeyTyped
        if ((!ftfBultos.getText().equals("0")) && (!ftfPerCada.getText().equals("0"))) {
            try {
                ftfTotal.setText(String.valueOf(Integer.parseInt(ftfPerCada.getText()) * Integer.parseInt(ftfBultos.getText())));
            } catch (Exception e) {
                ftfTotal.setText("0");
            }
        }
    }//GEN-LAST:event_ftfBultosKeyTyped

    private void llistaAbonosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_llistaAbonosValueChanged
        Abono abono = (Abono) llistaAbonos.getSelectedValue();
        if(abono != null){
            cbUnitat.setSelectedItem(abono.getUnitatsQuantitat());
            lblBultos.setText(abono.getUnitatsPaquet().getNom());
            ftfPerCada.setText(String.valueOf(abono.getQuantitatPaquet()));
            String nom = abono.getNom();
            if (nom != null) {
                lblNomAbono.setText(nom);
            }
            String composicio = abono.getComposicio();
            if (composicio != null) {
                lblComposicioAbono.setText(composicio);
            }
            String bulto = abono.getUnitatsPaquet().getNom();
            String quantitat = String.valueOf(abono.getQuantitatPaquet());
            String unitat = abono.getUnitatsQuantitat().getNom();
            if ((bulto != null) && (quantitat != null) && (unitat != null)) {
                lblQuantitatAbono.setText(bulto + " de " + quantitat + " " + unitat);
            }
        }
    }//GEN-LAST:event_llistaAbonosValueChanged

    private void lblMapaCampMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblMapaCampMouseClicked
        if (situarParcela == null) {
            List<Produccio> veins = produccions.findProduccioEntitiesByCamp(this.produccio.getCampId(), false);
            veins.remove(produccio);
            situarParcela = new DialogSituarParcela(this, true, this.produccio.getCampId(), produccio, veins, false);
        }
        situarParcela.setVisible(true);
    }//GEN-LAST:event_lblMapaCampMouseClicked

    private void btAvuiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAvuiActionPerformed
        dcDataAbonat.setDate(GregorianCalendar.getInstance().getTime());
}//GEN-LAST:event_btAvuiActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAceptar;
    private javax.swing.JButton btAvui;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btMenu;
    private javax.swing.JComboBox cbModeAplicacio;
    private javax.swing.JComboBox cbUnitat;
    private com.toedter.calendar.JDateChooser dcDataAbonat;
    private javax.swing.JFormattedTextField ftfBultos;
    private javax.swing.JFormattedTextField ftfPerCada;
    private javax.swing.JFormattedTextField ftfTotal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblBultos;
    private javax.swing.JLabel lblComposicioAbono;
    private javax.swing.JLabel lblMapaCamp;
    private javax.swing.JLabel lblNomAbono;
    private javax.swing.JLabel lblQuantitatAbono;
    private javax.swing.JList llistaAbonos;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object o1) {
        if (Abonat.class.isInstance(o1)) {
            try {
                update_Abonos();
                this.abonat = (Abonat) o1;
                produccio = this.abonat.getProduccioId();
                Utilitats.adabtarImatgeAlLabel(lblMapaCamp, produccio.getCampId().getImg());
                btMenu.setEnabled(false);
                List<Produccio> veins = produccions.findProduccioEntitiesByCamp(produccio.getCampId(), false);
                veins.remove(produccio);
                situarParcela = new DialogSituarParcela(this, true, produccio.getCampId(), produccio, veins, true);
                llistaAbonos.setSelectedValue(abonat.getAbonoId(), true);
                dcDataAbonat.setDate(abonat.getDataAbonat());
                ftfTotal.setText(String.valueOf(abonat.getQuantitat()));
                cbModeAplicacio.setSelectedItem(abonat.getModeAplicacioId());
            } catch (IOException ex) {
                Logger.getLogger(Abonar.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (Produccio.class.isInstance(o1)) {
            try {
                update_Abonos();
                this.abonat = null;
                this.produccio = (Produccio) o1;
                Utilitats.adabtarImatgeAlLabel(lblMapaCamp, produccio.getCampId().getImg());
                btMenu.setEnabled(false);
            } catch (IOException ex) {
                Logger.getLogger(Abonar.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
                update_Abonos();
                this.abonat = null;
                this.produccio = null;
                btMenu.setEnabled(true);

        }
    }

    private void update_Abonos() {
        llistaAbonos.setModel(getListModel(abonos.findAbonoEntitiesAZ()));
        dcDataAbonat.setDate(GregorianCalendar.getInstance().getTime());
        cbUnitat.setModel(Utilitats.getComboBoxModel(unitatsMesura.findUnitatsMesuraEntitiesForAbonoQuantitat()));
        lblBultos.setText(Textos.ABONAR_LABEL_BULTOS);
        ftfBultos.setText("0");
        ftfPerCada.setText("0");
        ftfTotal.setText("0");
        cbModeAplicacio.setModel(Utilitats.getComboBoxModel(modesAplicacio.findModeAplicacioEntities()));
        lblNomAbono.setText(" ");
        lblComposicioAbono.setText(" ");
        lblQuantitatAbono.setText(" ");
    }

    private DefaultListModel getListModel(List llista) {
        DefaultListModel modelP = new DefaultListModel();
        for (int i = 0; i < llista.size(); i++) {
            modelP.addElement(llista.get(i));
        }
        return modelP;
    }
}
